package com.nm.service.impl;

import com.nm.commons.exception.SupperException;
import com.nm.commons.util.StringUtil;
import com.nm.orm.dao.ArticleLabelRefDao;
import com.nm.orm.dao.LabelDao;
import com.nm.orm.model.State;
import com.nm.orm.pojo.ArticleLabelRefEntity;
import com.nm.orm.pojo.LabelEntity;
import com.nm.service.LabelService;
import com.nm.service.convert.LabelSvcConvert;
import com.nm.service.output.LabelIsUseOutput;
import com.nm.service.output.LabelOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 功能说明：标签service实现
 *
 * @return <br/>
 * 修改历史：<br/>
 * 1.[2017年02月12日上午17:27] 创建方法 by hw
 */
@Service
@Transactional
public class LabelServiceImpl implements LabelService {

    @Autowired
    private LabelDao lbDao;

    @Autowired
    private ArticleLabelRefDao alrDao;

    @Override
    public List<LabelOutput> findAll() throws SupperException {

        List<LabelEntity> labels = lbDao.findAll();

        List<LabelOutput> lbOuts = new ArrayList<LabelOutput>();
        LabelOutput output = null;
        for (LabelEntity label : labels) {
            output = new LabelOutput();

            output.setId(label.getId());
            output.setName(label.getName());

            lbOuts.add(output);
        }
        return lbOuts;
    }

    @Override
    public List<LabelIsUseOutput> findAll(String articleId) throws SupperException {

        List<LabelEntity> labels = lbDao.findAll();
        List<LabelIsUseOutput> labelOuts = LabelSvcConvert.conv(labels);
        //查询所有标签
        if (StringUtil.isNull(articleId)) {
            //bean转换
            return labelOuts;
        } else {
            //标签选择列表
            List<ArticleLabelRefEntity> alrs = alrDao.findAll(articleId);
            for (ArticleLabelRefEntity alr : alrs) {
                for (LabelIsUseOutput labelOut:labelOuts){
                    if (alr.getLabel().getId() == labelOut.getId()){
                        labelOut.setUse(true);
                        break;
                    }
                }
            }
        }
        return labelOuts;
    }

    @Override
    public String insert(LabelEntity labelEntity) {
        labelEntity.setState(State.ABLE.dbValue());
        labelEntity.setCreatedAt(new Date());
        return lbDao.insert(labelEntity);
    }

    // @Override
    // public void insert(List<LabelEntity> tes) {
    // lbDao.insert(tes);
    // }

    @Override
    public void saveOrUpdate(LabelEntity labelEntity) {
        if (StringUtil.isNull(labelEntity.getId())) {
            labelEntity.setCreatedAt(new Date());
        }

        if (State.DISABLE.dbValue() == labelEntity.getState()) {
            labelEntity.setState(State.ABLE.dbValue());
        }

        lbDao.saveOrUpdate(labelEntity);
    }

    @Override
    public void update(LabelEntity labelEntity) throws SupperException {

        if (State.DISABLE.dbValue() == labelEntity.getState()) {
            labelEntity.setState(State.ABLE.dbValue());
        }

        lbDao.saveOrUpdate(labelEntity);
    }

    @Override
    public LabelEntity getById(String id) throws SupperException {
        return lbDao.findById(id);
    }

    @Override
    public void delete(LabelEntity labelEntity) throws SupperException {
        lbDao.delete(labelEntity);
    }

//    @Override
//    public int deleteAll(LabelEntity labelEntity) throws SupperException {
//        return -2;
//    }

    @Override
    public int deleteById(String id) throws SupperException {
        return lbDao.deleteById(id);
    }
}
